package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.DeckInfoService;
import com.the4man.cnbdw.service.LogInfoService;
import com.the4man.cnbdw.utils.Page;
import com.the4man.cnbdw.utils.SelectModel;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.DataGrid;
import com.the4man.framework.model.DeckInfo;
import com.the4man.framework.model.DeckInfoExample;
import com.the4man.framework.model.LogInfo;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.util.RequestUtil;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Action(
        value = "deckInfoAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/deckInfo/add.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/deckInfo/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/deckInfo/edit.jsp"
        )}
)
public class DeckInfoAction extends BaseAction implements ModelDriven<DeckInfo> {
    private DeckInfo deckInfo = new DeckInfo();
    private LogInfo logInfo = new LogInfo();
    @Autowired
    private DeckInfoService deckInfoService;
    @Autowired
    private LogInfoService logService;

    public DeckInfoAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toAddPage() {
        return "addPage";
    }

    public String toEditPage() {
        try {
            DeckInfo d = (DeckInfo)this.deckInfoService.selectByPrimaryKey(this.getRequest().getParameter("id"), DeckInfo.class);
            BeanUtils.copyProperties(d, this.deckInfo);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        return "editPage";
    }

    public void getAllDatasourceList() {
        try {
            DeckInfoExample deckInfoExample = new DeckInfoExample();
            if(StringUtils.isNotEmpty(this.deckInfo.getName())) {
                deckInfoExample.createCriteria().andNameLike("%" + this.deckInfo.getName() + "%");
            }

            int total = this.deckInfoService.countByExample(deckInfoExample);
            Page<DeckInfo> page = new Page();
            this.setPageProperties(page);
            this.deckInfoService.selectByExample(deckInfoExample, page);
            DataGrid d = new DataGrid();
            d.setRows(page.getData());
            d.setTotal(Long.valueOf((long)total));
            this.writeJson(d);
        } catch (Exception var5) {
            LOG.error("获取数据失败", var5, new String[0]);
        }

    }

    public void add() {
        PageJson json = new PageJson();

        try {
            String name = this.deckInfo.getName();
            DeckInfo deck = this.deckInfoService.selectByName(name);
            if(deck == null) {
                String time = DateUtil.getInstance().formatDate(new Date());
                this.deckInfo.setId(UUID.randomUUID().toString());
                this.deckInfo.setCreateTime(time);
                this.deckInfo.setUpdateTime(time);
                this.deckInfo.setCreateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.deckInfo.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.deckInfoService.insert(this.deckInfo);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("甲板管理");
                this.logInfo.setOp("甲板添加");
                this.logInfo.setMemo("甲板:" + name);
                this.logService.insert(this.logInfo);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("甲板名已存在");
            }
        } catch (Exception var5) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var5, new String[0]);
        }

        this.writeJson(json);
    }

    public void update() {
        PageJson json = new PageJson();

        try {
            String name = this.deckInfo.getName();
            String id = this.deckInfo.getId();
            DeckInfo deck = this.deckInfoService.selectMByName(name, id);
            if(deck == null) {
                String time = DateUtil.getInstance().formatDate(new Date());
                this.deckInfo.setUpdateTime(time);
                this.deckInfo.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.deckInfoService.updateByPrimaryKey(this.deckInfo);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("甲板管理");
                this.logInfo.setOp("甲板修改");
                this.logInfo.setMemo("甲板:" + name);
                this.logService.insert(this.logInfo);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("甲板名已存在");
            }
        } catch (Exception var6) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var6, new String[0]);
        }

        this.writeJson(json);
    }

    public String executeModifyResult() {
        try {
            Object instance = this.deckInfoService.selectByPrimaryKey(this.deckInfo.getId(), DeckInfo.class);
            this.getRequest().setAttribute("instance", instance);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

        return "modifyPage";
    }

    public void delete() {
        PageJson json = new PageJson();

        try {
            int count = 0;
            String ids = this.getRequest().getParameter("ids");
            if(ids.contains(",")) {
                String[] str = ids.split(",");
                String[] var8 = str;
                int var7 = str.length;

                for(int var6 = 0; var6 < var7; ++var6) {
                    String info = var8[var6];
                    DeckInfo d = (DeckInfo)this.deckInfoService.selectByPrimaryKey(info, DeckInfo.class);
                    int i = this.deckInfoService.deleteByPrimaryKey(info, DeckInfo.class);
                    if(i > 0) {
                        this.logInfo.setId(UUID.randomUUID().toString());
                        this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                        this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                        this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                        this.logInfo.setFunc("甲板管理");
                        this.logInfo.setOp("甲板批量删除");
                        this.logInfo.setMemo("甲板:" + d.getName());
                        this.logService.insert(this.logInfo);
                        ++count;
                    }
                }
            } else {
                DeckInfo d = (DeckInfo)this.deckInfoService.selectByPrimaryKey(this.getRequest().getParameter("ids"), DeckInfo.class);
                count = this.deckInfoService.deleteByPrimaryKey(this.getRequest().getParameter("ids"), DeckInfo.class);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("甲板管理");
                this.logInfo.setOp("甲板删除");
                this.logInfo.setMemo("甲板:" + d.getName());
                this.logService.insert(this.logInfo);
            }

            if(count > 0) {
                json.setReturnMsg("成功删除" + count + "个甲板信息！");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("删除甲板信息失败！");
                json.setReturnFlag(false);
            }
        } catch (Exception var11) {
            LOG.error("批量删除甲板失败！", var11, new String[0]);
            json.setReturnMsg("删除甲板失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void verify() {
        PageJson json = new PageJson();
        this.writeJson(json);
    }

    public void getDeckInfoSelectData() {
        try {
            List<SelectModel> list = this.deckInfoService.getDeckInfoSelectData();
            this.writeJson(list);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

    }

    public DeckInfo getModel() {
        return this.deckInfo;
    }
}
